package com.project.communitymanagement.service.impl;

import com.project.communitymanagement.common.BaseServiceImpl;
import com.project.communitymanagement.controller.TbTurnoverDtsController;
import com.project.communitymanagement.entity.TbTurnover;
import com.project.communitymanagement.entity.TbTurnoverDts;
import com.project.communitymanagement.mapper.TbTurnoverDtsMapper;
import com.project.communitymanagement.mapper.TbTurnoverMapper;
import com.project.communitymanagement.service.TbTurnoverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ProjectName: epidemic-prevention-backend
 * @Package: com.project.communitymanagement.service.impl
 * @ClassName: TbTurnoverServiceImpl
 * @Author: "XGP"
 * @Description:
 * @Date: 2022/03/23 10:36
 * @Version: 1.0
 */
@Service
public class TbTurnoverServiceImpl  extends BaseServiceImpl<TbTurnoverMapper, TbTurnover> implements TbTurnoverService {

    @Autowired
    private TbTurnoverMapper tbturnoverMapper;
    @Autowired
    private TbTurnoverDtsMapper tbTurnoverDtsMapper;

    @Override
    protected TbTurnoverMapper mapper() {
        return tbturnoverMapper;
    }

    @Override
    protected TbTurnover defaultModel() {
        return new TbTurnover();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int goOut(TbTurnover tbTurnover) {
        TbTurnover goodTravel = tbturnoverMapper.getTbTurnoverByGoodTravel(tbTurnover.getGood_travel());
        if(goodTravel == null){
            throw new RuntimeException("当前出行号不存在,请确认后输入！");
        }
        if((goodTravel.getTurnover_sum() - goodTravel.getTurnover_number()) == 0){
            throw new RuntimeException("当前出行号已无次数,请重新申请！");
        }
        int updateCount = tbturnoverMapper.updateCount(tbTurnover.getGood_travel());
        TbTurnoverDts tbTurnoverDts = new TbTurnoverDts();
        tbTurnoverDts.setName(goodTravel.getName());
        tbTurnoverDts.setGood_travel(goodTravel.getGood_travel());
        tbTurnoverDts.setUsername(goodTravel.getUsername());
        tbTurnoverDts.setCard_number(goodTravel.getCard_number());
        tbTurnoverDts.setRemaining_count(goodTravel.getTurnover_sum() - (goodTravel.getTurnover_number() + 1));
        tbTurnoverDts.saveInit();
        tbTurnoverDtsMapper.insert(tbTurnoverDts);
        return updateCount;
    }
}

